/***************************************************************************
*  Edithistory.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include <QMessageBox>
#include "Edithistory.h"
EditHistory::EditHistory(QWidget *parent,Qt::WindowFlags f): QDialog(parent,f){
	 setupUi(this); 
    connect(pushButtonClear,SIGNAL(clicked()),this,SLOT(clearItem()));
    connect(pushButtonDel,SIGNAL(clicked()),this,SLOT(deleteItem()));
    connect(pushButtonSave,SIGNAL(clicked()),this,SLOT(saveHistoryfile()));
    connect(pushButtonRun,SIGNAL(clicked()),this,SLOT(historyChoosedToRun()));
    this->loadHistory();

    
}
void EditHistory::loadHistory(){
	 QString history=QString(getenv("HOME"))+QString("/.netflea/history.list");
    QFile file(history);
    if(file.open(QIODevice::ReadOnly)){
        QTextStream in(&file);
        QString tmpstring;
        while(!(in.atEnd())) {
            tmpstring = in.readLine();
            this->listWidget->addItem(tmpstring);
         }
        file.close();
    }
}
void EditHistory::deleteItem()
{
     QListWidgetItem * curitem = listWidget->takeItem(listWidget->currentRow());
     delete curitem;
}
void EditHistory::saveHistoryfile(){
	 QString history=QString(getenv("HOME"))+QString("/.netflea/history.list");
    QFile file(history);
    if(file.open(QIODevice::ReadWrite|QIODevice::Truncate)){
        QTextStream in(&file);
        for(int i=0;i<listWidget->count();i++){
             in<<listWidget->item(i)->text()<<"\n";
        }
        file.flush();
        file.close();
        QMessageBox::warning(this, tr("Information"),
             tr("History list have been saved!"));
       // this->accept();
        emit this->rollUp();
   }
    
}
void EditHistory::clearItem(){
	listWidget->clear();
}
void EditHistory::historyChoosedToRun(){
	emit this->runHistory((listWidget->currentItem())->text());
}